package com.example.servlet;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @BelongsProject: Java_learning
 * @BelongsPackage: com.example.servlet
 * @Author: song
 * @CreateTime: 2022-12-01  08:15
 * @Description: TODO
 * @Version: 1.0
 */
@WebServlet(name = "httpservlet",value = "/hs")
public class RegisterServlet extends javax.servlet.http.HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("测试成功！");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
//        System.out.println("这是get请求过来的内容！");
        resp.setCharacterEncoding("utf-8"); // 设置服务器飞返回格式
        //设置客户端的接受格式
        resp.setHeader("Content-Type","text/html;charset=utf-8");
        // 这一行代码跟上两行代码效果相同！
        resp.setContentType("text/html;charset=utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("提交的数据：" + username+"\t" +password );

        PrintWriter writer = resp.getWriter();
        writer.println("注册成功！");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doPost(req, resp);
//        System.out.println("这是post方法请求过来的内容！");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("提交的数据：" + username+"\t" +password );
    }
}
